<?php

class jwWidgetTableValues extends sfWidgetForm
{ 
  /**
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addOption( 'src', null );    
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The value displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render( $name, $value = null, $attributes = array(), $errors = array())
  {
    use_helper( 'Tag' );
    sfContext::getInstance()->getResponse()->addJavascript( "/jwTablePlugin/js/jwTable.js" );
    sfContext::getInstance()->getResponse()->addJavascript( "/jwTablePlugin/js/jwCell.js" );
    sfContext::getInstance()->getResponse()->addJavascript( "/jwTablePlugin/js/jwContext.js" );
    sfContext::getInstance()->getResponse()->addJavascript( "/jwTablePlugin/js/input.js" );
    
    sfContext::getInstance()->getResponse()->addStyleSheet( "/jwTablePlugin/css/table.css" );
    
    $id = get_id_from_name( $name );    
    $content = "<table id='".$id."' class='jwCustomTable'>";
    
    $values = json_decode( $value );
    
    if ( !count( $values ) )
    {
        $values = array( 0 => array( "&nbsp;", "&nbsp;" ), 1 => array( "&nbsp;", "&nbsp;" ) );
    }

    $rowCounter = 0;
    foreach( $values as $rowIndex => $row )
    {
        $colCounter = 0;        
        $content .= "<tr>";
        foreach( $row as $colIndex => $cell )
        {
            if( !( $colCounter && $rowCounter ) )
                $content .= "<th>".$cell."</th>";
            else
                $content .= "<td>".$cell."</td>";
            
            $colCounter++;            
        }
        $content .= "</tr>";
        $rowCounter++;

    }

    
    $content .= "</table>";
    $content .= "<script type='text/javascript' >";
    $content .= "new jwTable( document.getElementById( '".$id."'), { name: '".$name."' } );";
    $content .= "</script>";
    return $content;
  }    
    
}


?>